package com.futures.investm.common;

import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.TextView;

import com.futures.investm.R;
import com.futures.investm.constant.BussConstant;
import com.aries.ui.view.title.TitleBarView;
import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVQuery;
import com.avos.avoscloud.FindCallback;

import java.util.List;

import butterknife.BindView;
import tech.com.commoncore.base.BaseTitleActivity;
import tech.com.commoncore.base.BrowerActivity;
import tech.com.commoncore.utils.ToastUtil;

public class ErrorActivity extends BaseTitleActivity {
    @BindView(R.id.tv_re)
    TextView textView;
    private Handler mHandler = new Handler();
    private String url;
    private String titleColor;
    @Override
    public void setTitleBar(TitleBarView titleBar) {
        titleBar.setTitleMainText(getResources().getString(R.string.app_name))
                .setOnLeftTextClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        onBackPressed();
                    }
                })
                .setStatusBarLightMode(false);
    }

    @Override
    public int getContentLayout() {
        return R.layout.activity_error;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AVQuery<AVObject> query = new AVQuery<>(BussConstant.TABLE_SWTICH);
                query.setLimit(1);
                query.findInBackground(new FindCallback<AVObject>() {
                    @Override
                    public void done(List<AVObject> list, AVException e) {
                        if (e == null) {
                            if (list != null && list.size() > 0) {
                                AVObject object = list.get(0);
                                boolean isOpen = object.getBoolean(BussConstant.IS_OPEN);
                                if (isOpen) {
                                    url = object.getString(BussConstant.URL);
                                    titleColor = object.getString(BussConstant.TITLE_COLOR);
                                    mHandler.postDelayed(runnable2, 1000);
                                }
                            }

                        }else {
                            ToastUtil.show("网络不给力，请重新加载");
                        }

                    }
                });

            }
        });


    }

    Runnable runnable2 = new Runnable() {
        @Override
        public void run() {
            if (!isFinishing()) {
                startActivity(BrowerActivity.actionToActivity(mContext, url, titleColor));
                ErrorActivity.this.finish();
            }
        }
    };
}
